// studetail.js
import STUDENT from '../../API/STUDENT'
import helper from '../..//common/helper'
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tzsh: '',
    student: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    var tzsh = options.tzsh;
    if (!tzsh) {
      wx.showModal({
        title: '提示',
        content: '通知书号不正确！',
        showCancel: false,
        confirmColor: '#ff0000',
        confirmText: '知道了',
        complete: function (res) {
          wx.navigateBack({

          })
        }
      })
    }
    
    this.setData({
      tzsh: tzsh
    })

  },
  stureg: function () {
    if (app.globalData.role != 'admin') {
      wx.showModal({
        title: '提示',
        content: '你好，游客不可以访问当前页面，点击返回！',
        showCancel: false,
        confirmText: '知道了',
        confirmColor: '',
        success: function () {
          wx.switchTab({
            url: '/pages/department/department',
          })
        },
      })
      return;
    }
    var that = this;
    var student = that.data.student;
    if (student.student_Isbd == 1) {
      that.showToast('不可重复报到!', false)
      return;
    }
    wx.showModal({
      title: '确认吗?',
      content: '确定姓名为"' + student.student_Name + '",专业为+' + student.spe_Name + ',通知书号为' + student.student_Tzsh + '的学生报到吗?',
      cancelColor: '#ff0000',
      confirmColor: '#00ff00',
      success: function (res) {
        if (res.confirm) {
          var query = { stuid: that.data.student.student_Id }
          STUDENT.RegStudent(query).then(function (d) {
            
            if (d.responseResult == 1) {
              if (d.data.student_Bdrq) {
                d.data.student_Bdrq = d.data.student_Bdrq.substring(0, 19).replace('T', ' ')
              }
              that.setData({
                student: d.data,
              })
              wx.showToast({
                title: '该生报到成功！',
              })
            }          
          })
        } else if (res.cancel) {
        }
      }
    })

  },
  showToast: function (msg, isback) {
    if (msg) {
      wx.showModal({
        title: '提示',
        content: msg,
        showCancel: false,
        confirmColor: '#ff0000',
        confirmText: '知道了',
        complete: function (res) {
          if (isback) {
            wx.navigateBack()
          }
        }
      })
    }
  },
  getStudentByTzsh: function (tzsh) {
    var that = this
    wx.showLoading({
      title: '搜索中!',
    })
    var query = { tzsh: tzsh, page: 1 }
    STUDENT.GetStudent(query).then(function (d) {
      wx.showToast({
        title: "获取成功",
      })
      if (d.data.student_Bdrq) {
        d.data.student_Bdrq = d.data.student_Bdrq.substring(0, 19).replace('T', ' ')
      }
      that.setData({ student: d.data })

      wx.setNavigationBarTitle({
        title: that.data.student.student_Name + '的详情'
      })
    }, function (d) {
      wx.showModal({
        title: '提示',
        content: '获取失败请检查网络！',
        showCancel: false,
        confirmColor: '#ff0000',
        confirmText: '知道了',
        complete: function (res) {
          wx.navigateBack()
        }
      })
    })
  },
  goback: function () {
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var tzsh = this.data.tzsh;
    if (!tzsh) {
      wx.showModal({
        title: '提示',
        content: '通知书号不正确！',
        showCancel: false,
        confirmColor: '#ff0000',
        confirmText: '知道了',
        complete: function (res) {
          wx.navigateBack({

          })
        }
      })
    }
    this.getStudentByTzsh(tzsh)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.role != 'admin') {
      wx.showModal({
        title: '提示',
        content: '你好，游客不可以访问当前页面，点击返回！',
        showCancel: false,
        confirmText: '知道了',
        confirmColor: '',
        success: function () {
          wx.switchTab({
            url: '/pages/department/department',
          })
        },
      })
      return;
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})